﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using THSERP.BusinessEntities;
using THSLib;


namespace THSERP.BaseProvider
{
    public partial class guiLogin : Form
    {
        bool IsLogIn = false;
        public guiLogin()
        {
            InitializeComponent();
            //hieu 19/01/2012
            THSApp.RollbackLocalConnection();
            THSApp.IsSynchronizeDatabase = false;
            ADConfigValuesController objConfigValuesController = new ADConfigValuesController();
            DataSet ds = new HRShiftsController().GetAllObjects();
            fld_lkeHRShift.Properties.DataSource = ds.Tables[0]; 
            IsLogIn = false;
            fld_dtWorkingdate.DateTime = DateTime.Now;
        }

        #region Utility Functions
        public void CreateAdminUser()
        {
            ADUsersController objADUserController = new ADUsersController();
            if (objADUserController.GetObjectByName("admin") == null)
            {
                ADUsersInfo objADUsersInfo = new ADUsersInfo();
                objADUsersInfo.ADUserGroupID = 1;                
                objADUsersInfo.ADUserName = "admin";
                objADUsersInfo.ADPassword = "";
                objADUserController.CreateUser(objADUsersInfo);
            }
        }

        public bool IsAuthenticated(String strUserName, String strPassword)
        {
            ADUsersController objADUsersController = new ADUsersController();
            ADUsersInfo objADUsersInfo = new ADUsersInfo();
            objADUsersInfo = (ADUsersInfo)objADUsersController.GetObjectByName(strUserName.ToLower());
            if (objADUsersInfo != null)
            {
                String _encodedPassword = Convert.ToBase64String(SHA1Managed.Create().ComputeHash(ASCIIEncoding.ASCII.GetBytes(strPassword)));
                if (_encodedPassword.Equals(objADUsersInfo.ADPassword))
                    return true;

            }
            return false;
        }

        public bool IsUserLoggedIn(String strUserName)
        {
            //Get iADUserID
            ADUsersController objADUsersController = new ADUsersController();
            int iADUserID = objADUsersController.GetObjectIDByName(strUserName);

            GEUserAuditsController objGEUserAuditsController = new GEUserAuditsController();
            return objGEUserAuditsController.IsExistUser(iADUserID);
        }

        public void SetCurrentUserLogin(string strUserName)
        {
            ADUsersController objADUsersController = new ADUsersController();
            ADUsersInfo objADUsersInfo = new ADUsersInfo();
            objADUsersInfo = (ADUsersInfo)objADUsersController.GetObjectByName(strUserName);
            if (objADUsersInfo != null)
            {
                THSApp.CurrentUser = objADUsersInfo.ADUserName;
                THSApp.CurrentWorkingShift =(HRShiftsInfo) new HRShiftsController().GetObjectByID(Convert.ToInt32(fld_lkeHRShift.EditValue.ToString()));
                GELanguagesController objLanguageController = new GELanguagesController();
                GELanguagesInfo objLanguageInfo = new GELanguagesInfo();
                int iLanguageID = ((ADUserGroupsInfo)new ADUserGroupsController().GetObjectByID(objADUsersController.GetUserGroupOfUser(objADUsersInfo.ADUserID))).ADLanguageIDCombo;
                objLanguageInfo = (GELanguagesInfo)objLanguageController.GetObjectByID(iLanguageID);
                if (objLanguageInfo != null)
                    THSApp.CurrentLang = objLanguageInfo.GELanguageName.Trim();
            }
            THSApp.WorkingDate = fld_dtWorkingdate.DateTime.Date;
            THSApp.WorkingPeriod = THSApp.WorkingDate.Month;
            THSApp.WorkingYear = THSApp.WorkingDate.Year;
        }
        #endregion

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
           // this.Dispose();
            Application.ExitThread();
           // Application.Exit();
        }

        private void fld_btnLogin_Click(object sender, EventArgs e)
        {
            if (fld_lkeHRShift.EditValue == null)
            {
                MessageBox.Show("Chọn ca làm việc!");
                return;
            }
            if (Convert.ToBoolean(fld_chkForward.EditValue))
            {
                THSApp.SwitchConnectionNotCheckBranch(new BRBranchsController().GetMaxID()-1);
                THSApp.IsSynchronizeDatabase = true;
            }
             if (IsAuthenticated(fld_txtUserName.Text, fld_txtPassword.Text))
            {
                IsLogIn = true;
                SetCurrentUserLogin(fld_txtUserName.Text);
                this.DialogResult = DialogResult.OK;
                this.Dispose();
                
            }
            else
            {
              MessageBox.Show("Tên đăng nhập không đúng hoặc mật khẩu không chính xác!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //}
            //else
            //{
            //    MessageBox.Show("Tên đăng nhập hiện tại đang sử dụng hệ thống.","Log on", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
        }

        private void guiLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!IsLogIn)
                Application.ExitThread();
        }
        
    }
}